/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.agent;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JPanel;

import ca.usherbrooke.agent.action.Action;
import ca.usherbrooke.agent.controller.BasicAgentLauncher;
import ca.usherbrooke.behaviour.MyAutonomousBehaviour;
import ca.usherbrooke.goal.BasicGoal;
import ca.usherbrooke.mainApp.view.agent.EntityPanel;
import ca.usherbrooke.mainApp.view.agent.PanelAgent;
import ca.usherbrooke.mainApp.view.agent.PanelListGoalSolved;
import ca.usherbrooke.ui.MyPanelListGoal;

/**
 * @author Cody Stoutenburg
 * 
 */
public class SimpleAutonomousAgent extends SimpleMoveAgentController {
	private static final long serialVersionUID = 1L;

	private HashMap<String, ArrayList<Action>> memory;
	private Long currentGoalId;

	/**
	 * @param agent
	 */
	public SimpleAutonomousAgent(BasicAgentLauncher agent) {
		super(agent);
	}

	@Override
	public void initEntity() {
		super.initEntity();

		if (memory == null) {
			memory = new HashMap<String, ArrayList<Action>>();
		}
		this.addBehaviour(new MyAutonomousBehaviour(this));

		final PanelAgent<SimpleAutonomousAgent> panelAgent = new PanelAgent<SimpleAutonomousAgent>(
				this, new EntityPanel(this), new MyPanelListGoal(this),
				new PanelListGoalSolved<SimpleAutonomousAgent>(this));

		JPanel pane = null;
		while (pane == null) {
			pane = getEntityPanel();
			if (pane != null) {
				pane.add(panelAgent);
			}

			try {
				Thread.sleep(10);
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}
	}

	@Override
	public boolean haveMemory() {
		return true;
	}

	public void addMemory(String key, ArrayList<Action> plan) {
		memory.put(key, plan);
	}

	public List<Action> getMemory(String key) {
		return memory.get(key);
	}

	public List<Action> removeMemory(String key) {
		return memory.remove(key);
	}

	@Override
	public void saveMemory(File f) throws FileNotFoundException, IOException {
		ObjectOutputStream output = new ObjectOutputStream(
				new FileOutputStream(f));
		System.out.println("memory: " + memory.toString());
		output.writeObject(memory);
		output.flush();
		output.close();
	}

	@Override
	@SuppressWarnings("unchecked")
	public void loadMemory(File f) throws FileNotFoundException, IOException {
		ObjectInputStream obj = new ObjectInputStream(new FileInputStream(f));
		try {
			memory = (HashMap<String, ArrayList<Action>>) obj.readObject();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		} catch (ClassCastException ex) {
			ex.printStackTrace();
		}
		obj.close();
	}

	public final BasicGoal getSelectedGoal() {
		BasicGoal goal = getGoals().getGoal(currentGoalId);
		if (goal == null) {
			goal = getGoals().getFinishGoal(currentGoalId);
		}
		return goal;
	}

	public final void setSelectedGoal(Long guid) {
		currentGoalId = guid;
	}
}
